/****************************************************
 *	File Name:	dbx32sql.dll
 *	Class Name: Connect
 *	Description:
 *		Public Members of IsOpen(), Open() and Close() - 
 *		each of these items will do it's seperate function
 *		IsOpen():
 *			Determins if the Connection State of m_conn is Open returns
 *			a boolean for determination.
 * 
 *		Open():
 *			Firsts check to see if the connection state of m_conn is open,
 *			then if it's not it opens the connection of m_conn,
 *			returns true to say that it is open.
 * 
 *		Close():
 *			First determins if the connection state is open or closed,
 *			If it's open then it will Close the connection
 *			Returns true to say that the connection has been closed
 * 
 *	Date of Creation: 9/8/05
 *	LEAD Programmer: Dan Rux, Jr.
 ****************************************************/

using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Diagnostics;
using System.Security.Permissions;
using System.Security.Principal;

namespace dbx32sql
{
	/// <summary>
	/// Database connection manipulation.
	/// </summary>
	public class Connect
	{
		#region Private Variables

		private string database;		// Database name inputed from program.
		private string server;			// Server name - Is set up in the setup program, and is recalled by the program
		private string sapwd;			// The Sql Authentication Password for user SA.
		private SqlConnection m_conn;   // Sql Database Connection parameter.
		private string connectionString; // The string used by m_conn for the DB Connection

		#endregion

		#region Constructors

		/// <summary>
		/// Constructor - Input Server name, Database name and SAPWD password
		/// </summary>
		public Connect(string _database, string _server, string _sapwd)
		{
			this.database = _database;
			this.server = _server;
			this.sapwd = _sapwd;
			this.setConnection();
		}

		/// <summary>
		/// Standard Constructor - No Inputs, allows to set inputs by calling ConDb for\n
		/// Connect Database; ConServ for Connection.server; ConSA for Connection.sapwd;
		/// </summary>
		public Connect()
		{

		}

		#endregion

		#region Public Members

		/// <summary>
		/// Determine if the Connection State of m_conn is open.\n
		/// No Inputs.
		/// </summary>
		/// <returns>true if state = open; false if state = closed;</returns>
		public bool IsOpen()
		{
			if (m_conn.State == ConnectionState.Open)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// Opens the database connection.
		/// </summary>
		/// <returns>boolean true</returns>
		public bool Open()
		{
			bool Opened = false;
			try
			{
				if (IsOpen())
				{
					Opened = true;
				}
				else
				{
					m_conn.Open();
					Opened = true;
				}
				return Opened;
			}
			catch(Exception ex)
			{
				this.errTrack(ex);
				this.MessageError();
			}
			return Opened;
		}

		/// <summary>
		/// Closes the connection to the database.
		/// </summary>
		/// <returns>boolean true;</returns>
		public bool Close()
		{
			bool Closed = false;
			try
			{
				if (IsOpen())
				{
					m_conn.Close();
					Closed = true;
				}
				else
				{
					Closed = true;
				}
			}
			catch(Exception ex)
			{
				this.errTrack(ex);
				this.MessageError();
			}
			return Closed;
		}

		public void setConnection()
		{
			connectionString = string.Format("server={0};database={1};user id=sa;pwd={2}", server, database, sapwd);
			m_conn = new SqlConnection(connectionString);
		}

		#endregion

		#region Private Members

		private void errTrack(Exception ex)
		{
			string[] computer = WindowsIdentity.GetCurrent().Name.ToString().Split('\\');

			if (!EventLog.SourceExists("sqlConnection"))
			{
				EventLog.CreateEventSource("sqlConnection", "db error", computer[0]);
			}

			EventLog evntLog = new EventLog();
			evntLog.Source = "sqlConnection";
			evntLog.WriteEntry("Error 1005\n\n" + ex.Message, EventLogEntryType.Error);
			evntLog.Close();
		}

		private void MessageError()
		{
			DialogResult dlg = MessageBox.Show("Error 1005:\n\nDatabase error. Please see your Event Log for more information.", "Error 1005", MessageBoxButtons.OK, MessageBoxIcon.Error);
		}

		#endregion

		#region Public Delegates

		/// <summary>
		/// Returns the SqlConnection m_conn
		/// </summary>
		public SqlConnection Connection
		{
			get{ return m_conn; }
		}
		
		/// <summary>
		/// Gets or sets the value for the database name
		/// </summary>
		public string Database
		{
			get{ return database; }
			set{ database = value; }
		}

		/// <summary>
		/// gets or sets the value for the server name
		/// </summary>
		public string Server
		{
			get{ return server; }
			set{ server = value; }
		}

		/// <summary>
		/// Gets or sets the value for the SAPWD password
		/// </summary>
		public string Sapwd
		{
			get{ return sapwd; }
			set{ sapwd = value; }
		}

		#endregion
	}
}
